home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1997 August / Walnut Creek CDROM.7z / LISTINGS / V_13_04 / CHAPMAN2 / CHAPMAN2.ZIP / TSTERR.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-19  |  4.9 KB  |  155 lines

  1. /* tsterr.cpp - test error manager */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <ctype.h>
  6. #include "errmgr.hpp"
  7.  
  8.  
  9. static int ask(const char *prompt);
  10. static int write_complaint_file(const char *filename,
  11.                                 const char **msgs);
  12.  
  13. const int NAME_LEN = 32;
  14.  
  15.  
  16. /* we write more than one error dictionary to test the LIFO code: messages */
  17. /* in later dictionaries override the ones in earlier dictionaries. */
  18.  
  19. static const char *msgs1[] = {
  20.     "override: this message should have been overridden!\n",
  21.     "first_dict: this message came from the first dictionary defined - OK\n",
  22.     "one_of_each: %d int, %ld long, %lx long, %u unsigned - OK\n",
  23.     0
  24. };
  25.  
  26. static const char *msgs2[] = {
  27.     "override: this message overrides one in the first file - OK\n",
  28.     "malformed: this text doesn't match the format specification\n",
  29.     "second_of_each: %15.5e float, %6.4f float, %8g float - OK\n",
  30.     "third_of_each: %c char, %i int, %o octal, percent %% - OK\n",
  31.     0
  32. };
  33.  
  34. /* msgs3[] has some errors in it and so won't be installed by err_mgr. */
  35. /* we test this by defining a key here that's also in msgs2[] or msgs1[] */
  36. /* and writing an error that uses that key. */
  37.  
  38. static const char *msgs3[] = {
  39.     "override: this message should never be printed.\n",
  40.     "abcdef missing ':'\n",
  41.     0
  42. };
  43.  
  44.  
  45. int main(int argc,char *argv[])
  46. {
  47.     int errs = 0;
  48.     char tempname1[NAME_LEN],tempname2[NAME_LEN],tempname3[NAME_LEN];
  49.     error_mgr *bad_err_mgr;
  50.  
  51.     /* allocating an error_mgr will cause the program to halt. thus, we */
  52.     /* don't want to do it every time. */
  53.  
  54.     if (ask("Allocate a second error_mgr")) {
  55.         bad_err_mgr = new error_mgr;
  56.         fprintf(stderr,"Was able to allocate a second error_mgr!\n");
  57.         return EXIT_FAILURE;
  58.     }
  59.  
  60.     /* create three dictionary files, one of which has some errors in it. */
  61.     /* we assume the complaint dictionary code (complain.cpp) has been */
  62.     /* tested already (tstcompl.cpp) and so the dictionaries have rather */
  63.     /* ordinary messages in them. */
  64.  
  65.     tmpnam(tempname1);
  66.     if (write_complaint_file(tempname1,msgs1))
  67.         return EXIT_FAILURE;
  68.     tmpnam(tempname2);
  69.     if (write_complaint_file(tempname2,msgs2))
  70.         return EXIT_FAILURE;
  71.     tmpnam(tempname3);
  72.     if (write_complaint_file(tempname3,msgs3))
  73.         return EXIT_FAILURE;
  74.  
  75.     if (!err_mgr.define_dictionary(tempname1) ||
  76.         !err_mgr.define_dictionary(tempname2) ||
  77.         err_mgr.define_dictionary(tempname3)) {
  78.         fprintf(stderr,"define_dictionary() failed\n");
  79.         ++errs;
  80.     }
  81.  
  82.     /* now print messages to test the keyed error message facility. */
  83.     /* most of these will be overridden. every warning printed should */
  84.     /* have "OK" at the end of it. */
  85.  
  86.     err_mgr.warn("$override: failed to replace key!\n");
  87.     err_mgr.warn("$first_dict: failed to replace key!\n");
  88.     err_mgr.warn("$blot: this message should not be replaced - OK\n");
  89.     err_mgr.warn("%s - OK\n","An unkeyed message");
  90.  
  91.     /* this message, not the one in the dictionary, should be printed. */
  92.     /* the dictionary form is missing the %s. */
  93.  
  94.     err_mgr.warn("$malformed: has a string in it - %s\n","OK");
  95.  
  96.     /* bad message text - can't determine the key */
  97.  
  98.     err_mgr.warn("$x+y: bad key name - OK\n");
  99.     err_mgr.warn("$: missing key name - OK\n");
  100.  
  101.     /* print something of everything - %d, %c, %ld, %u, %e, %f, %g */
  102.  
  103.     err_mgr.warn("$one_of_each: int %3d, long %ld, long %lx, unsigned %u\n",
  104.                  34,12345678L,0x123456,0x8000);
  105.     err_mgr.warn("$second_of_each: float %10.5e, float %5.3f, float %10g\n",
  106.                  7.35e-10,3.75,27.1234);
  107.     err_mgr.warn("$third_of_each: char %c, int %i, octal %o\n",
  108.                  63,756,03577);
  109.  
  110.     /* test the new post() routine. */
  111.  
  112.     err_mgr.post("a posted message: %d - OK\n",5);
  113.  
  114.     remove(tempname1);
  115.     remove(tempname2);
  116.     remove(tempname3);
  117.  
  118.     if (!errs)
  119.         printf("all tests succeeded\n");
  120.     return errs ? EXIT_FAILURE : EXIT_SUCCESS;
  121.  
  122. }  /* end of main() */
  123.  
  124. static int ask(const char *prompt)
  125. {
  126.     /* ask for permission to do the requested task. the default (<CR>) */
  127.     /* is yes. */
  128.  
  129.     char buf[80];
  130.  
  131.     fprintf(stderr,"%s [yes]? ",prompt);
  132.     fgets(buf,sizeof(buf),stdin);       /* can't always read from stderr */
  133.     return toupper(buf[0]) != 'N';
  134.  
  135. }  /* end of ask() */
  136.  
  137. static int write_complaint_file(const char *filename,
  138.                                 const char **msgs)
  139. {
  140.     /* write the text to the named file. return 1 on failure, 0 on success. */
  141.  
  142.     int i;
  143.     FILE *tempfile;
  144.  
  145.     if ((tempfile = fopen(filename,"w")) == NULL) {
  146.         fprintf(stderr,"Unable to create temporary file\n");
  147.         return 1;
  148.     }
  149.     for (i = 0; msgs[i]; ++i)
  150.         fputs(msgs[i],tempfile);
  151.     fclose(tempfile);
  152.     return 0;
  153.  
  154. }  /* end of write_complaint_file() */
  155.